/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.common.service.serviceImpl;

import com.bjboot.caocao.system.dao.DictMapper;
import com.bjboot.caocao.system.domain.DictDO;
import com.bjboot.caocao.system.domain.UserDO;
import com.bjboot.caocao.common.service.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *@Description 数据字典实现类
 *@Author 操文健
 *@Date 2018-09-07 上午11:03
 *@Version 1.0
 **/
@Service
public class DictServiceImpl implements DictService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DictMapper dictMapper;

    @Override
    public DictDO get(Long id) {
        return dictMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DictDO> list(Map<String, Object> map) {
        return dictMapper.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return dictMapper.count(map);
    }

    @Override
    public int save(DictDO dict) {
        return dictMapper.insertSelective(dict);
    }

    @Override
    public int update(DictDO dict) {
        return dictMapper.updateByPrimaryKeySelective(dict);
    }

    @Override
    public int remove(Long id) {
        return dictMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchRemove(Long[] ids) {
        return dictMapper.batchRemove(ids);
    }

    @Override
    public List<DictDO> listType() {
        return dictMapper.listType();
    }

    @Override
    public String getName(String type, String value) {
        Map<String, Object> param = new HashMap<String, Object>(16);
        param.put("type", type);
        param.put("value", value);

        List<DictDO>  dictDOList = dictMapper.list(param);
        if (StringUtils.isEmpty(dictDOList)) {
            logger.info("[" + param + "]查询的字典值为空");
            return "";
        } else {
            String valueName = dictDOList.get(0).getName();
            return valueName;
        }
    }

    @Override
    public List<DictDO> getHobbyList(UserDO userDO) {
        // 爱好列表
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "hobby");
        List<DictDO> hobbyList = dictMapper.list(param);

        if (!StringUtils.isEmpty(userDO.getHobby())) {
            String userHobbys[] = userDO.getHobby().split(";");
            for (String userHobby : userHobbys) {
                for (DictDO hobby : hobbyList) {
                    if (!Objects.equals(userHobby, hobby.getId().toString())) {
                        continue;
                    }
                    // 针对不通用户的爱好匹配上的设置为true
                    hobby.setRemarks("true");
                    break;
                }
            }
        }

        return hobbyList;
    }

    @Override
    public List<DictDO> getSexList() {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", "sex");
        return dictMapper.list(param);
    }

    @Override
    public List<DictDO> listByType(String type) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("type", type);
        return dictMapper.list(param);
    }
}
